package com.util;

import com.alibaba.druid.pool.DruidDataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;

public class MyDruid {
    static ApplicationContext applicationContext;
    static DataSource dataSource;
    static {
        applicationContext = new
                ClassPathXmlApplicationContext("spring.xml");
        dataSource = (DataSource)
                applicationContext.getBean("MyDruid");
    }
    @Test
    public void T1() throws Exception {
        //创建数据源
        DruidDataSource dataSource = new DruidDataSource();
        //设置数据库连接参数
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/mydb");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        //获得连接对象
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
    }
    @Test
    public void T2() throws Exception{
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
    }
}
